var Action = {};
Action.pay = 'pay';
Action.push_role_login_out = 'push_role_login_out';
Action.role_login = 'role_login';
Action.role_level = 'role_level';
Action.role_create = 'role_create';
Action.weixin_share = 'weixin_share';
Action.check_share = 'check_share';
Action.init_share = 'init_share';
Action.open_share = 'open_share';
Action.init_code = 'init_code';
Action.open_code = 'open_code';
Action.bind_weixin = 'bind_weixin';
Action.back = 'back';
Action.reload = 'reload';
Action.loginout = 'loginout';
Action.clearCache = 'clearCache';
Action.hideGameList = 'hideGameList';
Action.getsetting = 'getsetting';
Action.downloadclient = 'downloadclient';
Action.share = 'share';
Action.focus = 'focus';
Action.realverify = 'realverify';
Action.openbbs = 'openbbs';
Action.opencomment = 'opencomment';
Action.openweb = 'openweb';
Action.show_adsense = 'show_adsense';
Action.create_adsense_order = 'create_adsense_order';
Action.get_role_transfer_list = 'get_role_transfer_list';
Action.update_role_transfer = 'update_role_transfer';
var GameUtil = {};
GameUtil.a = window;
GameUtil.b = document;
GameUtil.c = Math;
GameUtil.listener = function(callback) {
    console.log('开启监听');
    var handler = function(event) {
        GameUtil.a.removeEventListener('message', handler, false);
        callback(event)
    }
    if (typeof GameUtil.a.addEventListener != 'undefined') {
        GameUtil.a.addEventListener('message', handler, false)
    } else if (typeof GameUtil.a.attachEvent != 'undefined') {
        GameUtil.a.attachEvent('onmessage', handler)
    }
}
GameUtil.postMsg = function(action, data, callback) {
    GameUtil.listener(callback);
    var msg = {
        action: action,
        data: data
    };
    GameUtil.a.parent.postMessage(JSON.stringify(msg), "*")
}
GameUtil.postMsgSec = function(action, data) {
    var msg = {
        action: action,
        data: data
    };
    GameUtil.a.parent.postMessage(JSON.stringify(msg), "*")
}
var GameSdk = {};
GameSdk.pay = function(data, callback) {
    console.log('启用支付');
    GameUtil.postMsg(Action.pay, data, callback)
}
GameSdk.push_role_login_out = function(data, callback) {
    console.log('推送角色登出数据');
    GameUtil.postMsg(Action.push_role_login_out, data, callback)
}
GameSdk.push_role_login = function(data, callback) {
    console.log('推送角色登录数据');
    GameUtil.postMsg(Action.role_login, data, callback)
}
GameSdk.push_role_level = function(data, callback) {
    console.log('推送角色升级数据');
    GameUtil.postMsg(Action.role_level, data, callback)
}
GameSdk.push_role_create = function(data, callback) {
    console.log('推送角色创建数据');
    GameUtil.postMsg(Action.role_create, data, callback)
}
GameSdk.reload = function(callback) {
    console.log('刷新');
    GameUtil.postMsg(Action.reload, {
            msg: 'ok'
        },
        callback)
}
GameSdk.loginout = function(callback) {
    console.log('退出');
    GameUtil.postMsg(Action.loginout, {
            msg: 'ok'
        },
        callback)
}
GameSdk.clearCache = function(callback) {
    console.log('清除缓存');
    GameUtil.postMsg(Action.clearCache, {
            msg: 'ok'
        },
        callback)
}
GameSdk.hideGameList = function(callback) {
    console.log('隐藏游戏列表');
    GameUtil.postMsg(Action.hideGameList, {
            msg: 'ok'
        },
        callback)
}
GameSdk.WeixinShareCallBack = function(callback) {
    console.log('监听微信分享');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.weixin_share) {
            callback(result.data)
        }
    })
}
GameSdk.CheckShareCallBack = function(callback) {
    console.log('监听是否分享');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.check_share) {
            callback(result.data)
        }
    })
}
GameSdk.InitShare = function(callback) {
    console.log('初始化分享');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.init_share) {
            console.log('初始化分享回调');
            console.log(result);
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.init_share, {
        msg: 'ok'
    })
}
GameSdk.OpenShare = function(callback) {
    console.log('打开分享提示');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.open_share) {
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.open_share, {
        msg: 'ok'
    })
}
GameSdk.InitShowCode = function(callback) {
    console.log('初始化关注');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.init_code) {
            console.log('初始化关注回调');
            console.log(result);
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.init_code, {
        msg: 'ok'
    })
}
GameSdk.OpenShowCode = function(callback) {
    console.log('打开关注二维码');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.open_code) {
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.open_code, {
        msg: 'ok'
    })
}
GameSdk.isBindWeiXin = function(callback) {
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.bind_weixin) {
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.bind_weixin, {
        msg: 'ok'
    })
}
GameSdk.Back = function(callback) {
    console.log('监听后退');
    GameUtil.listener(function(event) {
        var result = JSON.parse(event.data);
        if (result.action == Action.back) {
            callback(result.data)
        }
    });
    GameUtil.postMsgSec(Action.back, {
        msg: 'ok'
    })
}
GameSdk.getsetting = function(callback) {
    GameUtil.postMsg(Action.getsetting, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.getsetting) {
                callback(result.data)
            }
        })
}
GameSdk.showAdsense = function(callback) {
    GameUtil.postMsg(Action.show_adsense, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.show_adsense) {
                callback(result.data)
            }
        })
}
GameSdk.createAdsenseOrder = function(data, callback) {
    GameUtil.postMsg(Action.create_adsense_order, data,
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.create_adsense_order) {
                callback(result.data)
            }
        })
}
GameSdk.get_role_transfer_list = function(data, callback) {
    GameUtil.postMsg(Action.get_role_transfer_list, data,
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.get_role_transfer_list) {
                callback(result.data)
            }
        })
}
GameSdk.update_role_transfer = function(data, callback) {
    GameUtil.postMsg(Action.update_role_transfer, data,
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.update_role_transfer) {
                callback(result.data)
            }
        })
}
GameSdk.downloadclient = function(callback) {
    GameUtil.postMsg(Action.downloadclient, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.downloadclient) {
                callback(result.data)
            }
        })
}
GameSdk.share = function(data, callback) {
    GameUtil.postMsg(Action.share, data,
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.share) {
                callback(result.data)
            }
        })
}
GameSdk.focus = function(callback) {
    GameUtil.postMsg(Action.focus, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.focus) {
                callback(result.data)
            }
        })
}
GameSdk.realverify = function(callback) {
    GameUtil.postMsg(Action.realverify, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.realverify) {
                callback(result.data)
            }
        })
}
GameSdk.openbbs = function(callback) {
    GameUtil.postMsg(Action.openbbs, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.openbbs) {
                callback(result.data)
            }
        })
}
GameSdk.opencomment = function(callback) {
    GameUtil.postMsg(Action.opencomment, {},
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.opencomment) {
                callback(result.data)
            }
        })
}
GameSdk.openweb = function(data, callback) {
    GameUtil.postMsg(Action.openweb, data,
        function(event) {
            var result = JSON.parse(event.data);
            if (result.action == Action.openweb) {
                callback(result.data)
            }
        })
}